package com.mijie.homi.admin.web.index;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mijie.core.ApplicationProperties;
import com.mijie.core.Paging;
import com.mijie.homi.admin.domain.commom.ActiveStat;
import com.mijie.homi.admin.domain.commom.Channel;
import com.mijie.homi.admin.domain.commom.Stat;
import com.mijie.homi.admin.domain.commom.StatByChannel;
import com.mijie.homi.admin.service.cache.MemcacheService;
import com.mijie.homi.admin.service.channel.ChannelService;
import com.mijie.homi.admin.service.commom.StatService;
import com.mijie.homi.common.constant.MCKey;


@Controller
@RequestMapping("/")
public class IndexController {

	@Autowired
	private StatService statService;
	@Autowired
	private ChannelService channelService;
	@Autowired
	private MemcacheService mcs;
	private Logger logger = Logger.getLogger(IndexController.class);
			
	@RequestMapping("index")
	public String index(HttpSession session){
		Object logged = session.getAttribute("logged");
		if(logged==null){
			return "redirect:/login.shtml";
		}
		return "index";
	}
	
	@RequestMapping(value="/importLastMood",method=RequestMethod.GET)
	public @ResponseBody void importLastMood() throws Exception{
		Connection conn= DriverManager.getConnection("proxool.maindb");;
		Statement moodStmt = null;
		Statement userStmt = null;
		int index = 0;
		try{
			moodStmt = conn.createStatement();
			userStmt = conn.createStatement();
			ResultSet rs = moodStmt.executeQuery("select m.id,m.mood,m.userId from mood as m inner join userExtInfo as u on m.id=u.moodId and u.moodId>0");
			int userId = 0;
			while(rs.next()){
				userId = rs.getInt("userId");
				userStmt.executeUpdate("update userExtInfo set mood="+rs.getInt("mood")+" where id="+userId);
				mcs.delete(MCKey.USER_EXTINFO_BY_ID, String.valueOf(userId));
				index++;
				if(index % 500 == 0)
					logger.info("importLastMood index:"+index);
			}
		}catch(Exception e){
			logger.error(e.getMessage(), e);
		}finally{
			if(moodStmt!=null) moodStmt.close();
		}
		logger.info("========importLastMood done===========");
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("list")
	public String List(Paging<Stat> paging,Model model,HttpServletRequest request){
		Map<String,Object> searchBundle = (Map<String, Object>) request.getAttribute("searchBundle");
		statService.listStat(paging, searchBundle);
		model.addAttribute("paging", paging);
		Stat sum = statService.sumStat(searchBundle);
		model.addAttribute("sum", sum);
		model.addAllAttributes(searchBundle);
		return "list";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("activeStat")
	public String activeStat(Paging<ActiveStat> paging,Model model,HttpServletRequest request){
		Map<String,Object> searchBundle = (Map<String, Object>) request.getAttribute("searchBundle");
		statService.listActiveStat(paging, searchBundle);
		model.addAttribute("paging", paging);
		model.addAllAttributes(searchBundle);
		return "activeStat";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("statByChannel")
	public String statByChannel(Model model,HttpServletRequest request){
		Map<String,Object> searchBundle = (Map<String, Object>) request.getAttribute("searchBundle");
		StatByChannel stat = statService.listStatByChannel(searchBundle);
		model.addAttribute("stat", stat);
		List<Channel> channelList = channelService.listChannel();
		model.addAttribute("channelList", channelList);
		model.addAllAttributes(searchBundle);
		return "statByChannel";
	}
	
	@RequestMapping("login")
	public String login(){
		return "login";
	}
	
	@RequestMapping("verify")
	public String verify(HttpSession session,String userName,String password){
		logger.info("login verify userName:"+userName+",password:"+password);
		if(userName!=null && password!=null &&
				userName.trim().equals(ApplicationProperties.get("admin.id")) && password.trim().equals(ApplicationProperties.get("admin.passwd"))){
			session.setAttribute("logged", "yes");
			return "redirect:/index.shtml";
		}
		return "redirect:/login.shtml";
	}
	
	@RequestMapping("logout")
	public String logout(HttpSession session){
		session.removeAttribute("logged");
		return "redirect:/login.shtml";
	}
}
